package com.neutech.web;



import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOImpl;
import com.neutech.entity.User;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
 * ${陈虎}
 * 14:46
 * wp_servlet_day01
 * ${"Do what you say,say what you do"}
 */




@WebServlet(name = "userServlet", urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet {
    private UserDAO dao = new UserDAOImpl();

    public UserServlet() {
        System.out.println("对象已经实例化");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*System.out.println("findAll().....");
        findAll(resp);*/
        // 进行前端用户请求的拦截
        // 根据用户请求url不同,进行不同的业务逻辑操作
        String pathUrl = request.getRequestURL().toString();
//        String pathUri = request.getRequestURI();
        pathUrl = pathUrl.substring(pathUrl.lastIndexOf("/"), pathUrl.lastIndexOf("."));
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        if ("/findAll".equals(pathUrl)) {
            List<User> users = dao.selectAll();
//            转发:业务逻辑处理未完成,交给更加便于编写样式的jsp页面显示数据
//            1.绑定数据
            request.setAttribute("users",users);
//            2.获取转发器
            RequestDispatcher rd = request.getRequestDispatcher("user_list.jsp");
//            3.转发
            rd.forward(request,response);
            /*out.print("<table align='center' width='80%' cellspacing='0' cellpadding='0'>");
            out.print("<tr><th>ID</th><th>用户名</th><th>密码</th><th>电话</th><th>地址</th><th>注册时间</th><th>修改时间</th><th>状态</th></tr>");
            for (User user : users) {
                out.print("<tr>" +
                        "<td>" + user.getId() + "</td>" +
                        "<td>" + user.getName() + "</td>" +
                        "<td>" + user.getPassword() + "</td>" +
                        "<td>" + user.getPhone() + "</td>" +
                        "<td>" + user.getAddress() + "</td>" +
                        "<td>" + user.getCreateTime() + "</td>" +
                        "<td>" + user.getUpdateTime() + "</td>" +
                        "<td>" + user.getStatus() + "</td></tr>");
            }
                out.println("</table>");*/
        }else if ("/insert".equals(pathUrl)) {
            String name= request.getParameter("name");
            String password= request.getParameter("password");
            String phone= request.getParameter("phone");
            String address= request.getParameter("address");
            dao.insert(new User(name,password,phone,address));

            response.sendRedirect("findAll.user");
        } else {
            /*System.out.println("路径错误...");
            response.getWriter().println("<h1>404</h1>");*/
            // 重定向: 业务逻辑处理完成后,页面跳转的过程
            response.sendRedirect("404.jsp");
        }
    }

    public void findAll (HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDAO dao = new UserDAOImpl();
        List<User> users = dao.selectAll();
        for (User user : users) {
            out.print("<p>" + user + "</p>");
        }
    }
    public void insert () {
        UserDAO dao = new UserDAOImpl();
        dao.insert(new User("", "", "", ""));
    }
}